package com.android.xunxian.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**工具类
 * Created by zouqh on 2016/5/23.
 */
public class Util {

    SharedPreferences sp;
    public String EmpId ="EmpId";//登录账号
    private String EmpName ="EmpName";// 姓名
    private String RoleId  ="RoleId";// 角色编号
    private String RoleName="RoleName";//角色名称
    private String pass="password";//密码
    private String name="name";//密码
    private String IP="ip";//服务器ip
    private String mindPass="mindpass";//记住密码
    private String post="post";//端口号
    public Util(Context context){
        sp = context.getSharedPreferences("userInfo", 1);
    }
    public static boolean logFlag=true;
    /**
     * 是否输入日志
     *
     * @param tag
     * @param info
     */
    public static void printLog(String tag, String info) {
        if (logFlag) {
            Log.i(tag, info + "");

        }
    }

    /**
     * 判断是否为空
     * @param str
     * @return
     */
    public static boolean NoNull(String str) {
        if (null != str && !str.equals("null") &&!str.equals("") && str.trim().length() > 0) {
            return true;
        }

        return false;

    }

    // 存单用户登录信息
    public void setUser(String... value) {
        SharedPreferences.Editor editor = sp.edit();
        if(value.length==1){
            editor.putString(name, value[0]);
        }else  if(value.length==2){
            editor.putString(IP, value[0]);
            editor.putString(post, value[1]);
        }else  if(value.length==3){
            editor.putString(name, value[0]);
            editor.putString(pass, value[1]);
            editor.putString(mindPass, value[2]);
        }

        editor.commit();
    }

    public void setTime(String time,String value){
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(time, value);
        editor.commit();
    }

    /**
     * 确定信息
     * @param name
     * @return
     */
    public String getUser(String name){

        return sp.getString(name, "");
    }


    // 将字符串写入到文本文件中
    public void writeTxtToFile(String strcontent, String filePath, String fileName) {
        //生成文件夹之后，再生成文件，不然会出错
        makeFilePath(filePath, fileName);

        String strFilePath = filePath+fileName;
        // 每次写入时，都换行写
        String strContent = strcontent + "\r\n";
        try {
            File file = new File(strFilePath);
            if (!file.exists()) {
                Log.d("TestFile", "Create the file:" + strFilePath);
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            RandomAccessFile raf = new RandomAccessFile(file, "rwd");
            raf.seek(file.length());
            raf.write(strContent.getBytes());
            raf.close();
        } catch (Exception e) {
            Log.e("TestFile", "Error on write File:" + e);
        }
    }



    // 生成文件
    public  File makeFilePath(String filePath, String fileName) {
        File file = null;
        makeRootDirectory(filePath);
        try {
            file = new File(filePath + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }else{

                new File(filePath + fileName).createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    // 生成文件夹
    public static void makeRootDirectory(String filePath) {
        File file = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                file.mkdir();
            }
        } catch (Exception e) {
            Log.i("error:", e + "");
        }
    }

    //判断email格式是否正确
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);

        return m.matches();
    }


}
